<template>
  <div class="mtype-container" v-if="data">
    <van-swipe :loop="false" class="mtype-swipe" v-if="data.type">
      <van-swipe-item>
        <div class="mtype-swipe-item">
          <van-cell :title="data.type.name" :label="data.type.description" :border="false">
            <van-icon slot="right-icon" :name="data.type.icon" />
          </van-cell>
          <van-button round type="info" size="small">等待审核</van-button>
        </div>
      </van-swipe-item>
    </van-swipe>
    <div class="mtype-notice" v-if="data.tips">
      <van-cell :title="data.tips.title" icon="fire" :value="data.tips.text" />
    </div>
    <div class="mtype-privilege" v-if="data.type && data.privileges.length>0">
      <h1>
        {{data.name}}
        <small>尊享{{data.privileges.length}}项特权</small>
      </h1>
      <van-grid class="mtype-privilege-list" :border="false">
        <van-grid-item
          :icon="item.icon?item.icon:'diamond-o'"
          :text="item.name"
          v-for="(item, index) in data.privileges"
          :key="index"
        />
      </van-grid>
    </div>
    <div class="mtype-action" v-if="operator">
      <van-submit-bar
        v-if="operator.action=='payment' && order"
        :disabled="disableSubmit"
        :loading="showSubmiting"
        :price="order.price*100"
        button-text="立即缴纳"
        label="保证金："
        button-type="warning"
        @submit="onPaybond"
      >
        <span slot="tip">
          缴纳完成即可开通会员服务，同时代表您已同意
          <span>会员服务协议</span>
        </span>
      </van-submit-bar>
      <van-button
        v-else-if="operator.action=='view'"
        block
        @click="pushNativelink(operator.link)"
      >{{operator.text}}</van-button>
      <van-button
        v-else-if="operator.action=='update'"
        size="large"
        block
        @click="onSubmit"
      >{{operator.text}}</van-button>
    </div>
    <van-dialog
      v-model="congratulate"
      v-if="congratulate"
      :title="congratulate.title"
      :confirm-button-text="congratulate.button.text"
      :confirm-button-color="congratulate.button.color"
      @confirm="onStartMember"
    >
      <div class="congratulate-message">
        <p>{{congratulate.message}}</p>
        <van-image fit="contain" height="200" :src="congratulate.cover" />
      </div>
    </van-dialog>
  </div>
</template>

<script>
import {
  Row,
  Col,
  Cell,
  Button,
  CellGroup,
  Swipe,
  SwipeItem,
  Icon,
  Image,
  Lazyload,
  Dialog,
  Grid,
  GridItem,
  SubmitBar
} from 'vant'

export default {
  components: {
    [Row.name]: Row,
    [Col.name]: Col,
    [Cell.name]: Cell,
    [Button.name]: Button,
    [CellGroup.name]: CellGroup,
    [Swipe.name]: Swipe,
    [SwipeItem.name]: SwipeItem,
    [Icon.name]: Icon,
    [Image.name]: Image,
    [SubmitBar.name]: SubmitBar,
    [Grid.name]: Grid,
    [GridItem.name]: GridItem,
    [Dialog.Component.name]: Dialog.Component,
    Lazyload
  },

  data () {
    return {
      disableSubmit: false,
      showSubmiting: false,
      paymentFrameClosed: false,
      congratulate: false,
      data: {
        type: {},
        privileges: []
      }
    }
  },
  created () {
    this.onLoad()
  },
  watch: {
    paymentFrameClosed: {
      handler (curVal, oldVal) {
        console.log('paymentFrameClosed1', curVal)
        if (curVal === true) {
          this.showSubmiting = false
          this.disableSubmit = true
          console.log('paymentFrameClosed2')
          this.onCheckPaymentStatus()
        }
      },
      deep: true
    },
    congratulate: {
      handler (curVal, oldVal) {
        if (!curVal) {
          this.disableSubmit = false
          this.onLoad()
          console.log('no congratulate', curVal)
        } else {
          console.log('congratulate', curVal)
        }
      },
      deep: true
    }
  },
  computed: {
    order () {
      return this.data.bond
    },
    operator () {
      return this.data.operation
    }
  },

  methods: {
    onStartMember () {
      this.$router.replace({ name: 'commission' })
    },
    onCheckPaymentStatus () {
      this.axios
        .get('/member/bond/status', {
          params: { id: this.order.id },
          headers: { FastMode: true }
        })
        .then(response => {
          this.congratulate = response.data
        })
        .catch(function (error) {
          console.log(error)
        })
    },
    onLoad () {
      this.axios
        .get('/member')
        .then(response => {
          this.data = response.data
        })
        .catch(function (error) {
          console.log(error)
        })
    },
    onPaybond () {
      this.showPaymentControl()
    },
    showPaymentControl () {
      this.showSubmiting = true
      this.listenPaymentClose()
      console.log('调用原生支付弹框，支付订单ID：' + this.order.id)
      this.jsBridge.showPaymentControl(
        {
          order_id: this.order.id,
          scope: 'bond'
        },
        response => {
          this.paymentFrameClosed = false
          console.log('已调起原生支付框 paymentFrameClosed=>', response)
          if (response.status) {
            console.log(
              '已调起原生支付框 paymentFrameClosed=>',
              this.paymentFrameClosed
            )
          } else {
            this.$toast('支付发起失败，请重试~')
            this.showSubmiting = false
          }
        }
      )
    },
    listenPaymentClose () {
      this.jsBridge.closePaymentControl(() => {
        console.log('关闭了弹窗')
        this.paymentFrameClosed = true
        console.log('关闭了弹窗 paymentFrameClosed=>', this.paymentFrameClosed)
      })
    },
    getPrivileges (id) {
      this.axios
        .get('/member/privilege', {
          params: { id: id },
          headers: { FastMode: true }
        })
        .then(response => {
          this.privileges = response.data
        })
        .catch(function (error) {
          console.log(error)
        })
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="less" scoped>
.mtype-container {
  position: relative;
  overflow: hidden;
  width: 100%;
  min-height: 100%;
  &::before {
    position: absolute;
    left: -25%;
    top: -3px;
    width: 150%;
    height: 200px;
    background: #e3c184;
    border-radius: 0 0 100% 100%;
    content: "";
    border: 3px dashed rgba(255, 255, 255, 0.3);
    display: block;
  }
}
.van-submit-bar {
  &__text {
    text-align: left;
    padding: 0 15px;
    display: flex;
    align-items: center;
  }
  &__price {
    padding-left: 10px;
  }
}

.congratulate-message {
  text-align: center;
  font-size: 14px;
  color: #aaa;
  font-weight: 300;
  padding: 10px;
  .van-image {
    max-height: 200px;
  }
}
.mtype-action {
  padding: 15px;
  position: absolute;
  left: 0;
  bottom: 0;
  width: 100%;

  .van-button--default {
    background-color: #e3c184;
    border-color: #e3c184;
    color: #805916;
    border-radius: 5px;
  }
}
.mtype-swipe {
  margin: 0 0 15px;
  &-item {
    position: relative;
    padding: 20px;
    border-radius: 10px;
    overflow: hidden;
    background: rgb(214, 235, 254);
    background: -moz-linear-gradient(
      -45deg,
      rgba(214, 235, 254, 1) 0%,
      rgba(197, 228, 255, 1) 50%,
      rgba(179, 222, 255, 1) 100%
    );
    background: -webkit-linear-gradient(
      -45deg,
      rgba(214, 235, 254, 1) 0%,
      rgba(197, 228, 255, 1) 50%,
      rgba(179, 222, 255, 1) 100%
    );
    background: linear-gradient(
      135deg,
      rgba(214, 235, 254, 1) 0%,
      rgba(197, 228, 255, 1) 50%,
      rgba(179, 222, 255, 1) 100%
    );
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#d6ebfe', endColorstr='#b3deff',GradientType=1 );

    &__bg {
      width: 100%;
      height: 100%;
      position: absolute;
      left: 0;
      top: 0;
    }
    .van-cell {
      background: transparent;
      padding-left: 0;
      padding-right: 0;
      &__title {
        font-size: 16px;
        font-weight: 600;
        color: rgba(70, 135, 240, 1);
      }
      &__label {
        font-weight: 300;
        color: rgba(70, 135, 240, 0.5);
      }
      .van-icon {
        font-size: 50px;
      }
    }
  }
  /deep/ .van-swipe {
    &__track {
      height: 180px;
    }
    &-item {
      padding: 15px;
      &:nth-child(2) {
        .mtype-swipe-item {
          background: rgb(245, 223, 186);
          background: -moz-linear-gradient(
            -45deg,
            rgba(245, 223, 186, 1) 0%,
            rgba(236, 208, 161, 1) 50%,
            rgba(230, 195, 139, 1) 100%
          );
          background: -webkit-linear-gradient(
            -45deg,
            rgba(245, 223, 186, 1) 0%,
            rgba(236, 208, 161, 1) 50%,
            rgba(230, 195, 139, 1) 100%
          );
          background: linear-gradient(
            135deg,
            rgba(245, 223, 186, 1) 0%,
            rgba(236, 208, 161, 1) 50%,
            rgba(230, 195, 139, 1) 100%
          );
          filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#f5dfba', endColorstr='#e6c38b',GradientType=1 );

          .van-cell {
            &__title {
              color: rgba(117, 76, 8, 1);
            }
            &__label {
              color: rgba(117, 76, 8, 0.5);
            }
          }
          .van-button--info {
            background-color: rgba(117, 76, 8, 1);
            border-color: rgba(117, 76, 8, 1);
          }
        }
      }
      &:nth-child(3) {
        .mtype-swipe-item {
          background: rgb(57, 58, 62);
          background: -moz-linear-gradient(
            -45deg,
            rgba(57, 58, 62, 1) 0%,
            rgba(51, 50, 55, 1) 50%,
            rgba(42, 41, 46, 1) 100%
          );
          background: -webkit-linear-gradient(
            -45deg,
            rgba(57, 58, 62, 1) 0%,
            rgba(51, 50, 55, 1) 50%,
            rgba(42, 41, 46, 1) 100%
          );
          background: linear-gradient(
            135deg,
            rgba(57, 58, 62, 1) 0%,
            rgba(51, 50, 55, 1) 50%,
            rgba(42, 41, 46, 1) 100%
          );
          filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#393a3e', endColorstr='#2a292e',GradientType=1 );

          .van-cell {
            &__title {
              color: rgba(255, 255, 255, 1);
            }
            &__label {
              color: rgba(255, 255, 255, 0.5);
            }
          }
          .van-button--info {
            background-color: #e3c184;
            border-color: #e3c184;
            color: #7d530b;
          }
        }
      }
    }
  }
}

.mtype-notice {
  padding: 20px 15px;
  .van-cell {
    border-radius: 30px;
    background: #fcfaed;
    padding: 5px 10px;
    font-size: 12px;
    line-height: 24px;
    &__left-icon {
      color: #ff5544;
    }
    &__right-icon {
      color: #f1b249;
    }
    &__title {
      color: #6d511f;
    }
    &__value {
      color: #f1b249;
      flex: none;
    }
  }
}
.mtype-privilege {
  h1 {
    text-align: center;
    font-weight: 600;
    font-size: 18px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #7d5609;
    small {
      font-weight: normal;
      opacity: 0.9;
    }
    &::before,
    &::after {
      content: "";
      width: 3px;
      height: 10px;
      background: #dfc087;
      margin: 0 10px;
      border-radius: 3px;
      transform: rotate(30deg);
    }
  }
  &-list {
    /deep/ .van-grid-item__content {
      background: transparent;
    }
  }
}
</style>
